package leetcode;

import java.util.Iterator;
import java.util.function.Consumer;

public class PeekingIterator<T> implements Iterator<T>{

	//Iterator是没有实现peek函数的
	
	private T nextElement;
	private T curElement;
	
	private Iterator<T> iterator;
	
	public PeekingIterator(Iterator<T> iterator) {
		this.iterator = iterator;
		
		//如果初始一个元素也没有，nextEleemenTnull
		if(iterator.hasNext()){
			//这里要使用this.iteraor
			nextElement = this.iterator.next();
		}
	}

	@Override
	public boolean hasNext() {
		return nextElement != null;
	}

	@Override
	public T next() {
		//if(hasNext()){
			curElement = nextElement;
			if(iterator.hasNext()){
				nextElement = iterator.next();
			}else{
				nextElement = null;
			}
			return curElement;
		//}
//		return null;
	}

	public T peek(){
		return nextElement;
//		if(hasNext()){
//			return nextEleemenT;
//		}else{
//			return null;
//		}
	}
	
	//
	@Override
	public void remove() {
		iterator.remove();
	}

	@Override
	public void forEachRemaining(Consumer<? super T> action) {
		
	}
}
